package com.tbvtc.agriculturalcreditchainserver2025.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tbvtc.agriculturalcreditchainserver2025.entity.Crop;
import com.tbvtc.agriculturalcreditchainserver2025.entity.FarmlandInfo;
import com.tbvtc.agriculturalcreditchainserver2025.entity.User;
import com.tbvtc.agriculturalcreditchainserver2025.exception.FoodException;
import com.tbvtc.agriculturalcreditchainserver2025.mapper.FarmlandInfoMapper;
import com.tbvtc.agriculturalcreditchainserver2025.mapper.UserMapper;
import com.tbvtc.agriculturalcreditchainserver2025.service.ICropService;
import com.tbvtc.agriculturalcreditchainserver2025.service.IFarmlandInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tbvtc.agriculturalcreditchainserver2025.utils.ResultCodeEnum;
import com.tbvtc.agriculturalcreditchainserver2025.vo.LargeDateScreen.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 农田信息表 服务实现类
 * </p>
 *
 * @author syh
 * @since 2025-04-14
 */
@Service
public class FarmlandInfoServiceImpl extends ServiceImpl<FarmlandInfoMapper, FarmlandInfo> implements IFarmlandInfoService {

    @Autowired
    private FarmlandInfoMapper farmlandInfoMapper;
    @Resource
    private ICropService cropService;
    @Autowired
    private UserMapper userMapper;

    @Override
    public Map selectLandArea(Integer id) {
         LambdaQueryWrapper<FarmlandInfo> lambdaQueryWrapper = new LambdaQueryWrapper<FarmlandInfo>()
                 .select(FarmlandInfo::getLandsize, FarmlandInfo::getIsActive)
                 .eq(FarmlandInfo::getRoleId,id);
                 ;
        List<FarmlandInfo> farmlandInfos = farmlandInfoMapper.selectList(lambdaQueryWrapper);
        if (farmlandInfos.isEmpty()){
            throw new FoodException(ResultCodeEnum.APP_LAND_NOT_EXIST);
        }
        Integer landArea =0 ; // 弃用总面积
        Integer countLandArea=0  ; // 耕种面积
        Integer totalLandArea = 0 ;
        Integer decreasedLandArea=0 ;  //弃用面积

//        farmlandInfos.forEach(farmlandInfo -> {
//              if (1==farmlandInfo.getIsActive()){
//                  landArea += farmlandInfo.getLandsize();
//              }
//        });
        for (FarmlandInfo farmlandInfo : farmlandInfos) {
            if (0==farmlandInfo.getIsActive()){
                countLandArea += farmlandInfo.getLandsize();
              } else if (1==farmlandInfo.getIsActive()) {
                landArea+=farmlandInfo.getLandsize();
            }else {
                decreasedLandArea+=farmlandInfo.getLandsize();
            }
        }
        totalLandArea = landArea+countLandArea+decreasedLandArea+landArea;
        Map map=new HashMap();
        map.put("totalLandArea",totalLandArea);
        map.put("landArea",countLandArea);
        map.put("countLandArea",landArea);
        map.put("decreasedLandArea",decreasedLandArea);
        return map;
    }

    @Override
    public UserInfo selectUserInfo(Integer id) {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<User>()
                .select(User::getUsername,User::getPhone,User::getUserImage)
                .eq(User::getId,id);
//        List<User> users = userMapper.selectList(lambdaQueryWrapper);
        User user = userMapper.selectOne(lambdaQueryWrapper);
//        List<UserInfo> userInfos = BeanUtil.copyToList(users, UserInfo.class);
        UserInfo userInfo = BeanUtil.copyProperties(user, UserInfo.class);
        return userInfo;

    }

    @Override
    public String getLandNameByFoodName(String name) {
        return  cropService.getOne(new LambdaQueryWrapper<Crop>().
                eq(Crop::getName,name)).getLandName();
    }


}
